#-*-coding:utf-8-*-
import os
import sys
from PySide2.QtWidgets import *
from PySide2.QtGui import *
from PySide2.QtCore import *

class CreateDepartment(QWidget):
    department_Signal=Signal(list)
    def __init__(self,real_project_path):
        super(CreateDepartment,self).__init__()

        #初始化打开该窗口时，就从主窗口那边把当前选中的真实项目路径传过来
        self.real_project_path=real_project_path

        #全局变量
        current_dir = os.path.dirname(__file__)
        self.PROJECT_INFO_PATH = os.path.join(os.path.dirname(current_dir), 'projects')

        self.department_list=[]#创建的总部门数

        self.setWindowTitle('创建部门阶段')
        self.mainLayout=QVBoxLayout()
        self.mainLayout.setContentsMargins(30,30,30,20)
        self.mainLayout.setAlignment(Qt.AlignCenter)
        self.mainLayout.setSpacing(30)

        self.checkbox_list = []

        self.mp_checkbox=QCheckBox('     美术')
        self.mm_checkbox=QCheckBox('     跟踪')
        self.ani_checkbox=QCheckBox('     动画')
        self.env_checkbox=QCheckBox('     场景')
        self.efx_checkbox=QCheckBox('     特效')
        self.comp_checkbox=QCheckBox('     合成')
        self.mg_checkbox=QCheckBox('     包装')
        self.lgt_checkbox=QCheckBox('     灯光')


        self.custom_layout=QHBoxLayout()
        self.custom_label=QLabel('其他:')
        self.custom_edit=QLineEdit()
        self.custom_layout.addWidget(self.custom_label)
        self.custom_layout.addWidget(self.custom_edit)

        self.btn_layout=QHBoxLayout()
        self.OK_btn=QPushButton('创建')
        self.OK_btn.clicked.connect(self.createDepartment)
        self.NO_btn=QPushButton('取消')
        self.NO_btn.clicked.connect(self.closeWindow)
        self.btn_layout.addWidget(self.OK_btn)
        self.btn_layout.addWidget(self.NO_btn)


        self.mainLayout.addWidget(self.mp_checkbox)
        self.mainLayout.addWidget(self.mm_checkbox)
        self.mainLayout.addWidget(self.ani_checkbox)
        self.mainLayout.addWidget(self.env_checkbox)
        self.mainLayout.addWidget(self.efx_checkbox)
        self.mainLayout.addWidget(self.comp_checkbox)
        self.mainLayout.addWidget(self.mg_checkbox)
        self.mainLayout.addWidget(self.lgt_checkbox)
        self.mainLayout.addLayout(self.custom_layout)
        self.mainLayout.addLayout(self.btn_layout)

        self.setLayout(self.mainLayout)

        self.checkbox_list.append(self.mp_checkbox)
        self.checkbox_list.append(self.mm_checkbox)
        self.checkbox_list.append(self.ani_checkbox)
        self.checkbox_list.append(self.env_checkbox)
        self.checkbox_list.append(self.efx_checkbox)
        self.checkbox_list.append(self.comp_checkbox)
        self.checkbox_list.append(self.mg_checkbox)
        self.checkbox_list.append(self.lgt_checkbox)





    def createDepartment(self):

        exist_department=os.listdir(self.real_project_path)
        # print(exist_department)
        create_info=''
        temp_list=exist_department #在原来的部门列表中添加
        exist_info=''
        for i in self.checkbox_list:
            if i.isChecked():
                text = i.text().strip(' ')
                if text not in exist_department:
                    create_info+=text+' '
                    temp_list.append(text)
                    department=os.path.join(self.real_project_path,text)
                    os.makedirs(department)
                else:
                    exist_info+=text+' '

        if exist_info !='':
            QMessageBox.information(self,'提示','{} 已经存在'.format(exist_info))

        custom_department=self.custom_edit.text()
        if custom_department:
            custom_department_path=os.path.join(self.real_project_path,custom_department)
            if os.path.exists(custom_department_path):
                QMessageBox.information(self,'提示','{} 已存在'.format(custom_department))
            else:
                os.makedirs(custom_department_path)
                temp_list.append(custom_department)
                create_info += custom_department + ' '
        self.department_list=temp_list
        self.department_Signal.emit(self.department_list)

        if create_info !='':
            QMessageBox.information(self,'创建成功','创建 {} 成功'.format(create_info))

        self.close()

    def closeWindow(self):
        self.close()

if __name__ == '__main__':
    app=QApplication(sys.argv)
    c=CreateDepartment('xxx')
    c.show()
    sys.exit(app.exec_())